package slavik.voloshyn.vashmagazyn;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;

public class UI extends JFrame implements ActionListener
{
	private final Object monitor = new Object();
	private JButton buttonOK;
	private JTextField textFieldUrl;
	private JTextField textFieldCountPages;
	private JLabel labelURL;
	private JLabel lablePageCount;
	private JTextArea textAreaResult;
	private JLabel labelDownloadStatus;
	private ParsingFile parsingFile;
	private DownloadFile downloadFile;

	public UI()
	{
		super();
		setSize(420, 380);
		setLayout(null);
		setResizable(false);
		setVisible(true);

		setPositionFrame();
		initButtons();
		initLabels();
		initTextFilds();

	}

	private void initTextFilds()
	{
		textFieldUrl = new JTextField();
		textFieldUrl.setSize(200, 20);
		textFieldUrl.setLocation(200, 20);
		add(textFieldUrl);

		textFieldCountPages = new JTextField();
		textFieldCountPages.setSize(200, 20);
		textFieldCountPages.setLocation(200, 50);
		add(textFieldCountPages);

		textAreaResult = new JTextArea();
		JPanel panel = new JPanel();
		panel.setSize(380, 220);
		panel.setLocation(20, 120);
		panel.setLayout(new BorderLayout());
		JScrollPane scroll = new JScrollPane(this.textAreaResult);
		this.textAreaResult.setFont(new Font("Serif", Font.PLAIN, 14));
		panel.add(scroll, BorderLayout.CENTER);
		add(panel);

	}

	private void initLabels()
	{
		labelURL = new JLabel("URL:");
		labelURL.setSize(100, 20);
		labelURL.setLocation(20, 20);
		add(labelURL);

		lablePageCount = new JLabel("Count pages:");
		lablePageCount.setSize(100, 20);
		lablePageCount.setLocation(20, 50);
		add(lablePageCount);

		labelDownloadStatus = new JLabel("Load...");
		labelDownloadStatus.setSize(100, 20);
		labelDownloadStatus.setLocation(20, 100);
		add(labelDownloadStatus);
		labelDownloadStatus.setVisible(false);
	}

	private void initButtons()
	{
		buttonOK = new JButton("OK");
		buttonOK.setSize(200, 30);
		buttonOK.setLocation(200, 80);
		add(buttonOK);
		buttonOK.addActionListener(this);
	}

	private void setPositionFrame()
	{
		Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
		int centerX = (int) screenSize.getWidth() / 2;
		int centerY = (int) screenSize.getHeight() / 2;
		this.setLocation(centerX - this.getWidth() / 2,
		        centerY - this.getHeight() / 2);
	}

	@Override
	public void actionPerformed(ActionEvent action)
	{

		if (action.getActionCommand().equals("OK"))
		{

			if (parsingFile != null
			        && downloadFile != null
			        && (parsingFile.getState() == Thread.State.RUNNABLE || downloadFile
			                .getState() == Thread.State.RUNNABLE))
			{
				return;
			}

			textAreaResult.setText("");
			labelDownloadStatus.setVisible(true);
			parsingFile = new ParsingFile(monitor, textAreaResult,
			        labelDownloadStatus);
			parsingFile.start();

			downloadFile = new DownloadFile(monitor);
			downloadFile.start();
			textFieldCountPages.setText("");
			textFieldUrl.setText("");

		}

	}
}
